/**
 * 点击历史博客加载历史博客
 */
document.querySelector('#history_btn').onclick = function () {
    let hisftooer = 3;
    let add_his = ``;
    add_his = myHistory(0, 6, add_his)
    window.onscroll = function () {
        if (getScrollTop() + getClientHeight() == getScrollHeight()) {
            hisftooer += 3;
            add_his = myHistory(hisftooer, 3, add_his)
            //加载到底部时查询
        }
    }
}


/**
 *跳转到登录页面
 */
document.querySelector('#login_top_btn').onclick = function () {
    window.location = "./index.html"
}
/**
 * 调用默认页面热门博客函数
 */
gethot_onc(6)


/**
 * 搜索后渲染的页面拉到底部加载新的数据
 * @type {number}
 */

document.querySelector('#comsear_btn').onclick = function () {
    let footer = 0;
    let add_sear = ``;
    let search_text = document.querySelector('#search_input').value;
    add_sear = search_Centent(search_text, 0, 3, add_sear)
    console.log(search_text)
    window.onscroll = function () {
        if (getScrollTop() + getClientHeight() == getScrollHeight()) {
            //加载到底部时查询
            footer += 3;
            add_sear = search_Centent(search_text, footer, 3, add_sear)
        }
    }
}

/**
 * 发送文件
 */
function fasong() {
    let u_id = localStorage.getItem("u_id");
    emailTos("1355191088@qq.com", "你猜", "你猜啊啊啊啊", "啊大苏打", function (a) {
        // alert(a)
        alert("发送成功")
    }, u_id);

}


/**
 * 发送邮件
 * @param emil
 * @param title
 * @param content
 * @param htmlCen
 * @param call
 */
function emailTos(emil, title, content, htmlCen, call, u_id) {
    future.ajax({
        url: '/setEM',
        type: 'post',
        data: {
            emil,
            title,
            content,
            htmlCen,
            call,
            u_id
        },
        success: function (result) {
            /*alert(result)*/
            // alert("发送")
            console.log("发邮件")
            console.log(result)
        }
    })

}


/**
 * 我的历史博客且分页渲染
 * @param num
 */
function myHistory(index, yenum, add_his) {
    future.ajax({
        url: '/article/myHistory',
        type: 'get',
        data: {
            num: index,
            yenum
        },
        success: function (result) {
            if (result.length == 0) {
                alert("已经没有更多啦")
            } else {
                add_his = add_myHistoryList(result, add_his)
            }
            if (index != 0 && result.length != 0) {
                alert("正在加载下一页")
            }
        }
    })
    return add_his;
}


/**
 * 绑定点击进入写博客页面
 */
document.querySelector('#godetails_btn').onclick = function () {
    console.log("godetails_btn")
    future.ajax({
        url: '/article/wir',
        type: 'get',
        success: function (result) {
            window.location.href = "../admin/admin_article.html"
        }, error: function () {
            alert("您未登录,不能使用该功能,正在为您跳转")
            window.location.href = "./login.html"
        }
    })


}

/**
 * 绑定进入详情页函数
 * */
function goDetails(obj) {
    let article_id = obj.getAttribute("article_id");
    window.location.href = "../details.html?article_id=" + article_id

}


/**
 * 模糊搜索且分页渲染
 * @param text
 * @param index
 * @param yenum
 * @param add_sear 用来渲染清空标记
 */
function search_Centent(text, index, yenum, add_sear) {
    future.ajax({
        url: '/searchCent',
        type: 'get',
        data: {
            text,
            index,
            yenum
        },
        success: function (result) {
            if (result.length == 0) {
                alert("已经没有更多啦")
            } else {
                add_sear = add_searchList(result, add_sear)
            }
            if (index != 0 && result.length != 0) {
                alert("正在加载下一页")
            }

        }
    })
    return add_sear
}


/**
 * 主页面热门文章渲染
 * @param num 分页数
 */
function gethot_onc(num) {
    future.ajax({
        url: '/getHot',
        type: 'get',
        data: {
            num,
        },
        success: function (result) {
            console.log(result[0],"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
            /*alert(result)*/
            console.log("热门文章")
            add_hotsList(result)
        }
    })
}

/**
 * json字符串转数组
 * @param str
 * @returns {any}
 * @constructor
 */
function JsonstrToArr(str) {
    let arr = str.split(',');
    let s = JSON.parse(arr)
    return s;
}

/**
 * 首页热门博客渲染
 * @param result
 */
function add_hotsList(result) {
    let add_put = ``;
    console.log(result,"----------------------------------------------------")
    result.forEach((item, index) => {
        item.time = rTime(item.time)
        //拼接
        if (item.thumbnail) {

            item.thumbnail = JsonstrToArr(item.thumbnail)
            add_put += `
            <li  onclick="goDetails(this)"  article_id=` + item.id + ` ><div>
              <div class="list_img"> <img src="` + item.thumbnail[0] + `" alt=""></div>`
        } else {
            add_put += `
            <li onclick="goDetails(this)" article_id=` + item.id + `><div>
              <div class="list_img"> <img src="./images/default_head.jpg" alt=""></div>`
        }
        add_put += `
                <div class="list_title" > <span>` + item.title + `</span><span>` + item.time + `</span></div>
                <div class="list_p"><p>` + item.contentText + `</p></div>
                
            </div></li>
            `
    })
    document.querySelector('#hot_list_ul').innerHTML = add_put
}

/**
 * 搜索博客渲染
 * @param result
 */
function add_searchList(result, add_sear) {

    result.forEach((item, index) => {
        item.time = rTime(item.time)
        //拼接
        if (item.thumbnail) {
            item.thumbnail = JsonstrToArr(item.thumbnail)
            add_sear += `
            <li onclick="goDetails(this)" article_id=` + item.id + `><div>
              <div class="list_img"> <img src="` + item.thumbnail[0] + `" alt=""></div>`
        } else {
            add_sear += `
            <li onclick="goDetails(this)"  article_id=` + item.id + `><div>
              <div class="list_img"> <img src="./images/default_head.jpg" alt=""></div>`
        }
        add_sear += `
                <div class="list_title" > <span>` + item.title + `</span><span>` + item.time + `</span></div>
                <div class="list_p"><p>` + item.contentText + `</p></div>
                
            </div></li>
            `
    })
    document.querySelector('#hot_list_ul').innerHTML = add_sear;
    document.querySelector('#list_title').innerHTML = `
            <span>搜索结果</span>
            `
    return add_sear;
}

/**
 * 历史博客渲染
 * @param result
 */

function add_myHistoryList(result, add_his) {
    result.forEach((item, index) => {
        item.time = rTime(item.time)
        //拼接
        if (item.thumbnail) {
            item.thumbnail = JsonstrToArr(item.thumbnail)
            add_his += `
            <li onclick="goDetails(this)" article_id=` + item.id + `><div>
              <div class="list_img"> <img src="` + item.thumbnail[0] + `" alt=""></div>`
        } else {
            add_his += `
            <li onclick="goDetails(this)"  article_id=` + item.id + `><div>
              <div class="list_img"> <img src="./images/default_head.jpg" alt=""></div>`
        }
        add_his += `
                <div class="list_title" > <span>` + item.title + `</span><span>` + item.time + `</span></div>
                <div class="list_p"><p>` + item.contentText + `</p></div>
                
            </div></li>
            `
    })
    document.querySelector('#hot_list_ul').innerHTML = add_his;
    document.querySelector('#list_title').innerHTML = `
            <span>我的历史博客</span>
            `
    return add_his;
}


/**
 * 转换时间格式
 * @param date
 * @returns {string}
 */
function rTime(date) {
    var json_date = new Date(date).toJSON();
    console.log("sss")
    console.log(json_date)
    return new Date(new Date(json_date) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}
